package com.jeremyfeinstein.utils;

import android.util.Log;

import com.jeremyfeinstein.gson.Bean;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.StringReader;

/**
 * Created by Administrator on 2018/3/19.
 */

public class XmlUtils {

    //解析Xml返回对象
    public static Bean parseXMLWithPull(String xmlData) throws Exception {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser parser = factory.newPullParser();
        parser.setInput(new StringReader(xmlData));
        int eventType = parser.getEventType();
        String version = "";
        String path = "";
        while (eventType != XmlPullParser.END_DOCUMENT) {
            String nodeName = parser.getName();
            switch (eventType) {
                // 开始解析某个结点
                case XmlPullParser.START_TAG: {
                    if ("version".equals(nodeName)) {
                        version = parser.nextText();
                    } else if ("path".equals(nodeName)) {
                        path = parser.nextText();
                    }
                    break;
                }
                // 完成解析某个结点
                case XmlPullParser.END_TAG: {
                    if ("config".equals(nodeName)) {
                        Log.d("MainActivity", "version is " + version);
                        Log.d("MainActivity", "path is " + path);
                    }
                    break;
                }
                default:
                    break;
            }
            eventType = parser.next();
        }
        return new Bean(version, path);
    }
}
